package org.theonept.travian.t4a.ressource;

import org.theonept.travian.model.Argile;
import org.theonept.travian.model.Bois;
import org.theonept.travian.model.Cereal;
import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Fer;
import org.theonept.travian.model.Ressource;
import org.theonept.travian.model.Village;
import org.theonept.travian.t4a.R;
import org.theonept.travian.util.AsyncWeb;
import org.theonept.travian.util.T4AUtil;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RessourcesActivity extends Activity implements OnClickListener {

	private Village village;
	private Compte compte;
	private Ressource ressource;
	private Button buttonSubmit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ressources);
		// Show the Up button in the action bar.
		village = (Village) getIntent().getExtras().getSerializable("village");
		compte = (Compte) getIntent().getExtras().getSerializable("compte");
		ressource = (Ressource) getIntent().getExtras().getSerializable("ressource");
		TextView textType = (TextView) findViewById(R.id.ressourceName);
		TextView textLevel = (TextView) findViewById(R.id.ressourceLevel);
		textLevel.setText("lvl" + ressource.getLevel());
		if (ressource instanceof Fer) {
			textType.setText(R.string.fer);
		} else if (ressource instanceof Argile) {
			textType.setText(R.string.argile);
		} else if (ressource instanceof Bois) {
			textType.setText(R.string.bois);
		} else if (ressource instanceof Cereal) {
			textType.setText(R.string.cereal);
		}

		buttonSubmit = (Button) findViewById(R.id.buttonUpRessource);
		buttonSubmit.setOnClickListener(this);
		new RessourceDetailAsync(compte).execute();
		setupActionBar();

	}

	private void updateDetail(Boolean result) {
		TextView textProd = (TextView) findViewById(R.id.ressourceProduction);
		textProd.setText("Production: " + ressource.getProduction());
		TextView textCostFer = (TextView) findViewById(R.id.costFer);
		TextView textCostArgile = (TextView) findViewById(R.id.costArgile);
		TextView textCostBois = (TextView) findViewById(R.id.costBois);
		TextView textCostCereal = (TextView) findViewById(R.id.costCereal);
		TextView textConsoCereal = (TextView) findViewById(R.id.consoCereal);

		textCostFer.setText(ressource.getCostFer() + "");
		textCostBois.setText(ressource.getCostBois() + "");
		textCostArgile.setText(ressource.getCostArgile() + "");
		textCostCereal.setText(ressource.getCostCereal() + "");
		textConsoCereal.setText(ressource.getConsoCereal() + "");
		RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.panelUp);
		relativeLayout.setVisibility(View.VISIBLE);
		if (result) {
			buttonSubmit.setVisibility(View.VISIBLE);
		}
		T4AUtil.setTextViewRessources(this, ressource.getVillage().getProduction(false));
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ressources, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			fin();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		new ExecuteUpAsync(compte).execute();

	}

	class ExecuteUpAsync extends AsyncWeb<Compte, Void, Boolean> {

		public ExecuteUpAsync(Compte compte) {
			super(compte);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result) {
				fin();
			} else {
				Toast.makeText(getApplicationContext(), getText(R.string.cannot_upgrage_ressource), Toast.LENGTH_LONG);
			}

		}

		@Override
		protected Boolean executeWebTask(Compte... params) {

			return ressource.up();
		}
	}

	class RessourceDetailAsync extends AsyncWeb<Compte, Void, Boolean> {

		public RessourceDetailAsync(Compte compte) {
			super(compte);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			updateDetail(result);
			super.onPostExecute(result);

		}

		@Override
		protected Boolean executeWebTask(Compte... params) {
			ressource.getDetail();
			return ressource.isUpable();
		}

	}

	private void fin() {
		setResult(RESULT_OK, getIntent());
		finish();
	}
}
